package 每日一题;

import org.junit.Test;

import java.util.List;

public class 通过删除字母匹配到字典里最长单词 {
    //思路一样，实现的比我简洁
    public String findLongestWord2(String s, List<String> dictionary) {
        String res = "";
        for (String t : dictionary) {
            int i = 0, j = 0;
            while (i < t.length() && j < s.length()) {
                if (t.charAt(i) == s.charAt(j)) {
                    ++i;
                }
                ++j;
            }
            if (i == t.length()) {
                if (t.length() > res.length() || (t.length() == res.length() && t.compareTo(res) < 0)) {
                    res = t;
                }
            }
        }
        return res;
    }

    public String findLongestWord(String s, List<String> dictionary) {
        int num=0;
        String res="";
        for (String s1 : dictionary) {
            if (isSatisfy(s,s1) && s1.length()>num){
                num=s1.length();
                res=s1;
            }
            if (isSatisfy(s,s1) && s1.length()==num && s1.compareTo(res)<0){
                res=s1;
            }
        }
        return res;
    }
    //判断两个字符串是否相等
    public boolean isSatisfy(String s, String ans){
        if (s.equals(ans) || ans.equals(""))
            return true;
        int i=0,j=0;
        while (i<s.length() && j<ans.length()){
            if (s.charAt(i)==ans.charAt(j))
                j++;
            i++;
        }
        return j==ans.length();
    }
    @Test
    public void test(){
        System.out.println(isSatisfy("abc", "ab"));

        String a="abc";
        String b="abe";
        System.out.println(a.compareTo(b) < 0);
//        System.out.println(a);
    }
}
